package com.ruoyi.lowcode.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.lowcode.domain.VoteCandidate;
import com.ruoyi.lowcode.mapper.VoteActivityMapper;
import com.ruoyi.lowcode.domain.VoteActivity;
import com.ruoyi.lowcode.service.IVoteActivityService;
import com.ruoyi.common.core.text.Convert;

/**
 * 投票活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-28
 */
@Service
public class VoteActivityServiceImpl implements IVoteActivityService 
{
    @Autowired
    private VoteActivityMapper voteActivityMapper;

    /**
     * 查询投票活动
     * 
     * @param id 投票活动主键
     * @return 投票活动
     */
    @Override
    public VoteActivity selectVoteActivityById(Long id)
    {
        return voteActivityMapper.selectVoteActivityById(id);
    }

    /**
     * 查询投票活动列表
     * 
     * @param voteActivity 投票活动
     * @return 投票活动
     */
    @Override
    public List<VoteActivity> selectVoteActivityList(VoteActivity voteActivity)
    {
        return voteActivityMapper.selectVoteActivityList(voteActivity);
    }

    /**
     * 新增投票活动
     * 
     * @param voteActivity 投票活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVoteActivity(VoteActivity voteActivity)
    {
        int rows = voteActivityMapper.insertVoteActivity(voteActivity);
        insertVoteCandidate(voteActivity);
        return rows;
    }

    /**
     * 修改投票活动
     * 
     * @param voteActivity 投票活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVoteActivity(VoteActivity voteActivity)
    {
        voteActivityMapper.deleteVoteCandidateByActivityId(voteActivity.getId());
        insertVoteCandidate(voteActivity);
        return voteActivityMapper.updateVoteActivity(voteActivity);
    }

    /**
     * 批量删除投票活动
     * 
     * @param ids 需要删除的投票活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVoteActivityByIds(String ids)
    {
        voteActivityMapper.deleteVoteCandidateByActivityIds(Convert.toStrArray(ids));
        return voteActivityMapper.deleteVoteActivityByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除投票活动信息
     * 
     * @param id 投票活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVoteActivityById(Long id)
    {
        voteActivityMapper.deleteVoteCandidateByActivityId(id);
        return voteActivityMapper.deleteVoteActivityById(id);
    }

    /**
     * 新增候选人信息
     * 
     * @param voteActivity 投票活动对象
     */
    public void insertVoteCandidate(VoteActivity voteActivity)
    {
        List<VoteCandidate> voteCandidateList = voteActivity.getVoteCandidateList();
        Long id = voteActivity.getId();
        if (StringUtils.isNotNull(voteCandidateList))
        {
            List<VoteCandidate> list = new ArrayList<VoteCandidate>();
            for (VoteCandidate voteCandidate : voteCandidateList)
            {
                voteCandidate.setActivityId(id);
                list.add(voteCandidate);
            }
            if (list.size() > 0)
            {
                voteActivityMapper.batchVoteCandidate(list);
            }
        }
    }
}
